package com.momo.mal;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.momo.mal.dashboard.Dashboard;
import com.momo.mal.database.MalDatabaseAdapter;
import com.momo.mal.util.ConnectionUtil;
import com.momo.mal.util.Synchcronize;
import net.momo.mal.R;

public class Login extends SherlockActivity {

	public static final String PREFS_NAME = "CREDENTIALS";

	private CheckBox showPassword;
	private Button buttonLogin;
	private EditText username, password;
	private MalDatabaseAdapter dbAdapter;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.login);
		
		dbAdapter = new MalDatabaseAdapter(this);
		if (!getSharedPreferences(Login.PREFS_NAME, MODE_PRIVATE).getString(
				"username", "").isEmpty()) {
			if (dbAdapter.isAnimeDataExist() == false
					|| dbAdapter.isMangaDataExist() == false) {
				new Synchcronize(this).execute();
			}
			
			startActivity(new Intent(Login.this, Dashboard.class));
			finish();
		}
		
		buttonLogin = (Button) findViewById(R.id.button_login);
		username = (EditText) findViewById(R.id.edit_username);
		password = (EditText) findViewById(R.id.edit_password);
		showPassword = (CheckBox) findViewById(R.id.check_box_show_password);
		
		showPassword.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean status) {
				if(status) {
					password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
				} else {
					password.setInputType(129);
				}
			}
		});

		buttonLogin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				new RequestLogin(Login.this).execute(username.getText()
						.toString(), password.getText().toString());
			}
		});
		
	}

	private class RequestLogin extends AsyncTask<String, Void, Boolean> {

		private ProgressDialog dialog;
		private String username;
		private String password;
		private Context context;

		public RequestLogin(Context cxt) {
			dialog = new ProgressDialog(cxt);
			this.context = cxt;
		}

		@Override
		protected void onPreExecute() {
			dialog.setMessage("Signing in ...");
			dialog.show();
		}

		@Override
		protected Boolean doInBackground(String... params) {
			this.username = params[0];
			this.password = params[1];
			return ConnectionUtil.login(username, password);
		}

		@Override
		protected void onPostExecute(Boolean result) {
			dialog.dismiss();
			if (result) {
				SharedPreferences settings = getSharedPreferences(PREFS_NAME,
						MODE_PRIVATE);
				SharedPreferences.Editor editor = settings.edit();
				editor.putString("username", this.username);
				editor.putString("password", this.password);
				editor.commit();
				
				new Synchcronize(context).execute();
				startActivity(new Intent(Login.this, Dashboard.class));
				finish();
			} else {
				Toast.makeText(Login.this, "Wrong username or password",
						Toast.LENGTH_LONG).show();
			}
		}

	}
}
